#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>

int closeopenvpn(){
  char *filevpnname = "/usr/local/etc/mdemon/openvpn.pid";
  FILE *filevpn;
  filevpn = fopen(filevpnname, "r");
  if (filevpn == NULL)
  {
    perror("fopen:");
    return 1;
  }
  
  int pid;  
  if (fscanf(filevpn, "%d", &pid) != 1)
  {
    //printf("subor %s je prazdny alebo ma neplatny tvar", filevpnname);
    fclose(filevpn);
    unlink(filevpnname);
    return 1; 
  }
  else if (kill(pid, SIGKILL) == -1)
  {
    perror("kill:");
    fclose(filevpn);
    unlink(filevpnname);
    return 1; 
  }
  else
  {
    //printf("closeopenvpn succeeded\n");
    fclose(filevpn);
    unlink(filevpnname);
    return 0;
  }  
}
